package cn.yunhe.service.impl;

import cn.yunhe.dao.NewsMapper;
import cn.yunhe.entity.News;
import cn.yunhe.entity.Page;
import cn.yunhe.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service("newsService")
public class NewsServiceImpl implements INewsService {
    @Resource
    private NewsMapper newsMapper;

    @Override
    public Page queryLikeNews(Map<String, Object> cond) {
        Page page = new Page();
        page.setTotalNum(newsMapper.queryLikeNewsCount(cond)); //总记录数
        //每页显示的条数
        int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize")));
        page.setPageSize(pageSize);

        //当前页
        int currPageNum = Integer.parseInt(String.valueOf(cond.get("pageNum")));
        page.setCurPageNum(currPageNum);

        //计算总页数
        page.setTotalPages(page.getTotalNum() / pageSize + (page.getTotalNum() % pageSize == 0 ? 0 : 1));
        //记录列表
        page.setResult(newsMapper.getLikeNews(cond));
        return page;
    }

    @Override
    public Integer addNews(News news) {
        return  newsMapper.addNews(news);
    }

    @Override
    public Integer delNews(News news) {
        return newsMapper.delNews(news);
    }

    @Override
    public News getNewsById(News news) {
        return newsMapper.getNewsById(news);
    }

    @Override
    public Integer updateNews(News news) {
        return newsMapper.updateNews(news);
    }

    public NewsMapper getNewsMapper() {
        return newsMapper;
    }

    public void setNewsMapper(NewsMapper newsMapper) {
        this.newsMapper = newsMapper;
    }
}
